﻿using Aop.Api.Domain;
using Aop.Api.Request;
using Aop.Api.Response;
using Core.Framework.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Aop.Api
{


    public class Tool
    {
        /// <summary>
        /// 支付
        /// </summary>
        /// <param name="title">订单名称</param>
        /// <param name="orderKey">订单号</param>
        /// <param name="pay">支付金额</param>
        /// <param name="title">订单名称</param>
        /// <param name="alipay_url">服务地址</param>
        /// <param name="app_id">APPid</param>
        /// <param name="privtekey">私钥</param>
        /// <param name="publickey">公钥</param>
        /// <returns></returns>
        public string AliPay(string orderKey, string pay, string title, string alipay_url, string app_id, string privtekey, string publickey)
        {
            IAopClient client = new DefaultAopClient(
                alipay_url,
                app_id,
                privtekey,
				"json",
				"1.0",
				"RSA2",
                publickey,
				"utf-8",
				false);

            //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称如：alipay.trade.app.pay
            AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
			//SDK已经封装掉了公共参数，这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。
			AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
			model.Body = title;
			model.Subject = title; //"App支付测试DoNet"
			model.TotalAmount = pay;
			model.ProductCode = "QUICK_MSECURITY_PAY";
			model.OutTradeNo = "cloud__" + orderKey; // 订单号
			model.TimeoutExpress = "30m";

            request.SetBizModel(model);
			//这里和普通的接口调用不同，使用的是sdkExecute
			AlipayTradeAppPayResponse response = client.SdkExecute(request);
			return response.Body;
		}

        public string TransferByAccounts(string orderKey, string pay, string title, string alipay_url, string app_id, string privtekey, string publickey) 
        {
            IAopClient client = new DefaultAopClient(
                alipay_url,
                app_id,
                privtekey, 
                "json", "1.0", "RSA2",
                publickey,
                "utf-8",false);
            AlipayFundTransToaccountTransferRequest request = new AlipayFundTransToaccountTransferRequest();

            AlipayFundTransToaccountTransferModel model = new AlipayFundTransToaccountTransferModel();

            model.OutBizNo = "cloud__" + orderKey;

            model.PayeeType = "ALIPAY_LOGONID";

            model.PayeeRealName = "朱健";
            model.PayeeAccount = "15252156614";

            model.Amount = pay;

            // 付款方信息
            model.PayerShowName = "iTool Finance";

            model.Remark = "请及时查收订单货款，并进行核对。祝您生活愉快";
            model.ExtParam = new {
                order_title = title
            }.TryToJson();

            request.SetBizModel(model);

            //request.BizContent = "{" +
            //"\"out_biz_no\":\"3142321423432\"," +
            //"\"payee_type\":\"ALIPAY_LOGONID\"," +
            //"\"payee_account\":\"abc@sina.com\"," +
            //"\"amount\":\"12.23\"," +
            //"\"payer_real_name\":\"上海交通卡公司\"," +
            //"\"payer_show_name\":\"上海交通卡退款\"," +
            //"\"payee_real_name\":\"张三\"," +
            //"\"remark\":\"转账备注\"," +
            //"\"ext_param\":\"{\\\"order_title\\\":\\\"兑奖红包\\\"}\"" +
            //"  }";

            AlipayFundTransToaccountTransferResponse response = client.Execute(request);


            return response.Body;
        }

    }
}
